package com.hinner.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.hinner.common.JwtTokenUtils;
import com.hinner.common.Result;
import com.hinner.entity.User;
import com.hinner.entity.dto.UserDTO;
import com.hinner.entity.vo.*;
import com.hinner.service.CompanyService;
import com.hinner.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private CompanyService companyService;


    /**
     * 条件分页查询所有用户信息
     * @param queryParams
     * @return
     */
    @PostMapping("/getUserList")
    public Result findAllUsers(@RequestBody QueryUserVO queryParams) {
        PageInfo<User> allUsers = userService.findAllUsers(queryParams);
        return Result.success(allUsers);
    }


    /**
     * 注册新用户
     * @param userVO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserVO userVO){
        userService.register(userVO);
        return Result.success();
    }


    /**
     * 修改用户状态
     * @param
     * @param
     * @return
     */
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody User user){
        System.out.println("user = " + user);
        return Result.success(userService.updateStatus(user.getId(),user.getIsActived()));
    }

    /**
     * 用户登录
     * @param userVO
     * @return 完整的用户信息
     */
    @PostMapping("/login")
    public Result login(@RequestBody UserVO userVO){
        User user = userService.login(userVO);
        log.info(user !=null ? "成功":"失败");
        Map<String, Object> map = new HashMap<>();
        map.put("userInfo",user);
        map.put("token",JwtTokenUtils.genToken(user.getId().toString(), user.getPassword()));
        return Result.success(map);
    }




    /**
     * 修改用户信息接口
     * @param userVO
     * @return
     */
    @PostMapping("/modify")
    public Result modify(@RequestBody UserVO userVO){
        return Result.success(userService.modify(userVO));
    }

    /**
     * 根据id逻辑删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id){
        return Result.success(userService.removeById(id));
    }

    /**
     * 查询所有用户信息
     * @return
     */
    @GetMapping("/getAll")
    public Result getAll(){
        return Result.success(userService.list());
    }


    /**
     * 根据id查询单个用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        User user = userService.getById(id);
        user.setCompanyName(companyService.getById(user.getCompanyId()).getCompanyName());
        return Result.success(user);
    }


    /**
     * 忘记密码功能，根据邮箱号发邮件重置用户密码
     * @param
     * @param
     * @return
     */
    @PostMapping("/forgot-password")
    public Result forgotPassword(@RequestBody UserVO userVO) {
        boolean success = userService.forgotPassword(userVO.getUsername(), userVO.getEmail());
        return success ? Result.success("密码重置邮件已发送") : Result.error("用户名或邮箱不正确");
    }

    /**
     * 修改用户密码
     * @param vo
     * @return
     */
    @PostMapping("/modify-password")
    public Result modifyPassword(@RequestBody ModifyPasswordVO vo) {
        return userService.modifyPassword(vo) ?
                Result.success("密码修改成功") : Result.error("原密码不正确，密码修改失败");
    }

    /**
     * 随机获取一个管理员
     * @return
     */
    @GetMapping("/getRandomAdmin")
    public Result getRandomAdmin(){
        List<User> users = userService.list(new QueryWrapper<User>()
                .eq("role", "ROLE_ADMIN")
                .eq("isActived", 0));
        if (CollectionUtils.isEmpty(users)) return Result.error("暂无管理员");
        User user = users.get(0);
        HashMap<String, Object> map = new HashMap<>();
        map.put("email",user.getEmail());
        map.put("phone",user.getPhone());
        return Result.success(map);
    }

}
